﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace Fcdbas.Platform.Properties
{
    [TypeConverter(typeof(AllowedOperationsConverter))]
    public class AllowedOperationsProperty
    {
        public AllowedOperationsProperty()
        {
            this.New = true;
            this.Modify = true;
            this.Remove = true;
        }

        public AllowedOperationsProperty(bool[] allowed)
        {
            New = allowed[0];
            Modify = allowed[1];
            Remove = allowed[2];
        }

        public AllowedOperationsProperty(bool allowedNew, bool allowedModify, bool allowedRemove)
        {
            New = allowedNew;
            Modify = allowedModify;
            Remove = allowedRemove;
        }

        /// <summary>
        /// 允许新增
        /// </summary>
        public bool New
        {
            get;
            set;
        }

        /// <summary>
        /// 允许修改
        /// </summary>
        public bool Modify
        {
            get;
            set;
        }

        /// <summary>
        /// 允许删除
        /// </summary>
        public bool Remove
        {
            get;
            set;
        }
    }

    public class AllowedOperationsConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string)) return true;

            if (destinationType == typeof(InstanceDescriptor)) return true;

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            string result = string.Empty;
            if (destinationType == typeof(string))
            {
                
                AllowedOperationsProperty allowed = value as AllowedOperationsProperty;
                if (allowed == null)
                    return string.Empty;

                result = "New,Modify,Remove";

                if (!allowed.New)
                    result = result.Replace("New", "");
                if (!allowed.Modify)
                    result = result.Replace("Modify", "");
                if (!allowed.Remove)
                    result = result.Replace("Remove", "");

                return result;
            }

            if (destinationType == typeof(InstanceDescriptor))
            {
                //ConstructorInfo ci = typeof(AllowedOperationsProperty).GetConstructor(new Type[] { typeof(bool), typeof(bool), typeof(bool) });
                AllowedOperationsProperty allowed = (AllowedOperationsProperty)value;

                return new InstanceDescriptor(null, new object[] { allowed.New, allowed.Modify, allowed.Remove });
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value is string)
            {
                string valueObject = value as string;
                if (string.IsNullOrEmpty(valueObject))
                    return new AllowedOperationsProperty();

                string[] allowedValue = valueObject.Split(',');
                if (allowedValue.Length != 3)
                    throw new ArgumentException("无效的属性格式");

                AllowedOperationsProperty allowed = new AllowedOperationsProperty(allowedValue[0].ToLower() == "new" ? true : false, 
                                                                                  allowedValue[1].ToLower() == "modify" ? true : false,
                                                                                  allowedValue[2].ToLower() == "remove" ? true : false);

                return allowed;
            }

            return base.ConvertFrom(context, culture, value);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(AllowedOperationsProperty), attributes);
        }
    }
}
